package cn.edu.hizee.gps.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


public class BaseController {

    public String ajaxReturn(HttpServletResponse response, int resultcode, String info, Object data) {

        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            map.put("data", data);
            map.put("resultcode", resultcode);
            map.put("info", info);
            String json = JSON.toJSONString(map);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public String ajaxReturn(HttpServletResponse response, int resultcode, String info, Object data, String type) {

        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            if(type.equals("1")) {
                map.put("data2", data);
            }else if(type.equals("2")){
                map.put("data", data);
            }
            map.put("resultcode", resultcode);
            map.put("info", info);
            map.put("type", type);
            String json = JSON.toJSONString(map);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }


    public String ajaxReturn(HttpServletResponse response, int resultcode, String info, Object data, Object data2) {

        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            map.put("resultcode", resultcode);
            map.put("info", info);
            map.put("data", data);
            map.put("data2", data2);
            String json = JSON.toJSONString(map);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }


    public String ajaxReturn(HttpServletResponse response, int resultcode, String info, Object data, Integer userId, Object data2) {

        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            map.put("data", data);
            map.put("resultcode", resultcode);
            map.put("info", info);
            map.put("userId", userId);
            map.put("data2", data2);
            String json = JSON.toJSONString(map);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public String ajaxReturn(HttpServletResponse response, int resultcode, String info, Object data, Object user, Object data2,Integer userId, Object data_user_info) {

        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();
            HashMap<String, Object> map = new HashMap<>();
            map.put("data", data);
            map.put("resultcode", resultcode);
            map.put("info", info);
            map.put("user", user);
            map.put("userId", userId);
            map.put("data2", data2);
            map.put("data_user_info", data_user_info);
            String json = JSON.toJSONString(map);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }


    public String ajaxReturn(int resultcode, String info, Object data) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("data", data);
        map.put("resultcode", resultcode);
        map.put("info", info);
        String json = JSON.toJSONString(map);
        System.out.println(json);
        return json;
    }


    public String ajaxReturn(HttpServletResponse response, HashMap<String, Object> map) {
        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();

            String json = JSON.toJSONString(map);
            System.out.println(json);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public String ajaxReturn(HttpServletResponse response, List<?> list) {
        try {
            response.setCharacterEncoding("utf-8");

            PrintWriter pw = response.getWriter();

            String json = JSON.toJSONString(list);
            pw.print(json);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

}
